<template>
  <div class="modal-from">
    <a-modal
      :title="open_type == 'add' ? '新增班组' : '编辑班组' "
      :width="500"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="submit"
      @cancel="visible=false"
    >
      <div class="table-page-search-wrapper">
        <a-form :form="form" layout="inline">
          <a-row :gutter="24" type="flex" justify="center">

            <a-col :span="20">
              <a-form-item label="班组名称" has-feedback>
                <a-input placeholder="请输入" v-decorator="['teamName',{rules: [{ required: true, message: '请输入班组名称！' }]}]"/>
              </a-form-item>
            </a-col>
            <a-col :span="20">
              <a-form-item label="组长姓名" has-feedback>
                <a-select
                  :showSearch="true"
                  option-filter-prop="children"
                  allow-clear
                  style="width: 100%"
                  placeholder="选择,搜索"
                  v-decorator="['teamLeaderId', {rules: [{ required: true, message: '请选择姓名！' }]}]"
                >
                  <a-select-option v-for="(item,index) in userList" :key="index" :value="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
  </a-modal>
  </div>
</template>

<script>
import {AddTeamDefinition,EditTeamDefinition} from '@/api/bd/team'

import { AllHisUser1} from '@/api/customer/customer'

export default {
  props:{
    row_record:{
      type:Object,
      default:()=>{}
    },
    open_type :{
      type : String ,
      default : 'add'
    }
  },
  data(){
    return{
      userList:[],
      // 查询参数
      queryParam: {
        pageNum: 1,//第几页
        pageSize: 10,//每页中显示数据的条数
      },
      value: [],
      visible: false,
      visibleTable: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      pagination: {
        current: 1,//当前页
        pageSize: 10,//当前页数
        total: 0,//总条数
        showSizeChanger: true,
        showQuickJumper: true,
        pageSizeOptions: ['10', '20', '30'],//这里注意只能是字符串，不能是数字
        showTotal: total => `共有 ${ total} 条数据`,//分页中显示总的数据
      },
      customerDataTable:[],
      columns: [
        { title: '序号', customRender: (text, record, index)=>(this.pagination.current-1)*this.pagination.pageSize+(index+1) },
        { title: '姓名', dataIndex: 'name' },
        { title: '卡号', dataIndex: 'cardNo' },
        { title: '开发员', dataIndex: 'developer', customRender:age=>age?age:'无' },
        { title: '咨询师', dataIndex:'consultMan',customRender:consultMan=>{return consultMan?consultMan:'无'}},
        { title: '登记时间', dataIndex: 'recordDate', }
      ],
    }
  },
  created() {
    AllHisUser1().then(res=>{
      this.userList=res.userList
    })
  },
  methods: {
    // 初始化方法
    show() {
      this.visible = true
      this.$nextTick(()=>{
        if(this.open_type=='add'){
          this.form.resetFields()
          return
        }
        this.form.setFieldsValue({
          teamName:this.row_record.teamName,
          teamLeaderId:this.row_record.teamLeaderId,
        })
      })
    },
    submit() {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (errors) {
          console.log('校验未通过')
          this.confirmLoading = false
          return
        }
        console.log('校验通过', values)
        const findResult = this.userList.find(item=>item.id==values.teamLeaderId)
        if(findResult){
          values.teamLeader=findResult.name
        }
        if( this.open_type=='add'){
          AddTeamDefinition(values).then(res => {
            if (res.code == 200) {
              this.$message.success({
                content: '增加成功',
                duration: 0.5,
                onClose: () => {
                  this.visible = false
                  this.$emit('updateSuccess')
                }
              })
            } else {
              this.$message.info( res.message)
            }
          }).finally(()=>{
            this.confirmLoading = false
          })
          return
        }



        //修改
        values.tbNetTeamDefinitionId=this.row_record.tbNetTeamDefinitionId
        values.teamId=this.row_record.teamId
        EditTeamDefinition(values).then(res => {
          if (res.code == 200) {
            this.$message.success({
              content: '修改成功',
              duration: 0.5,
              onClose: () => {
                this.visible = false
                this.$emit('updateSuccess')
              }
            })
          } else {
            this.$message.info( res.message)
          }
        }).finally(()=>{
          this.confirmLoading = false
        })
      })
    },
    //分页动作监听
    handleTableChange(pagination) {
      this.pagination.current = pagination.current
      this.pagination.pageSize = pagination.pageSize
      this.queryParam.pageNum = pagination.current
      this.queryParam.pageSize = pagination.pageSize
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
      // 清理子表单中数据
      // 清理时间
    },
  }
}
</script>

<style scoped>
/deep/ .ant-card-head-title{
  font-weight: bold;
}
/deep/ .ant-page-header-heading-title{
  font-weight: normal;
  font-size: 16px;
}
/deep/ .ant-card-body {
  padding: 24px 12px 0 ;
}
/deep/ .ant-form-item-control-wrapper{
  width: 100%;
}
.input-search .has-success.has-feedback .ant-form-item-children-icon{
  top: 60%;
  right: 50px !important;
}

</style>